<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="操作人"></el-input>
				</el-form-item>
				<el-form-item>
					<el-date-picker type="date" placeholder="操作日期" format="yyyy-MM-dd"   v-model="filters.date" style="width: 100%;"></el-date-picker>
				</el-form-item>

				<el-form-item>
					<el-button type="primary" v-on:click="getPlatFromAccountRecord">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
				<el-form-item >
					<el-tag  v-if='isShowKey'  >{{filters.platAccount}}</el-tag>
				</el-form-item>
				<el-form-item>
					<el-button type="primary"  v-if='isShowKey'  v-on:click="findPlatFromAccount">查询平台余额</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="record" highlight-current-row v-loading="listLoading" border max-height="620"  style="width: 100%;">
			<el-table-column type="index" width="80" label="编号">
			</el-table-column>
			<el-table-column prop="user.username" label="操作人"    >
			</el-table-column>
			<el-table-column prop="changeamount" label="变动金额"  >
			</el-table-column>
			<el-table-column prop="changetime" label="变动时间"   >
			</el-table-column>

			<el-table-column prop="payType" label="变动类型" width="120" :formatter="formatPayType" >
			</el-table-column>
			<el-table-column prop="remark" label="备注" min-width="120"  >
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-pagination
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
				layout="total,sizes,prev, pager, next"
				:total="total">
		</el-pagination>

		<el-dialog title="新增支出金额" width="30%" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="120px" :rules="addFormRules" ref="addForm">

				<el-form-item label="支出金额" prop="username">
					<el-input v-model="addForm.price" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="备注">
					<el-input type="textarea" v-model="addForm.remark"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	import { getPlatFromAccountRecord ,findPlatFromAccount,addPlatFormAccount} from '../../api/api';
    import moment from 'moment';
	export default {
		data() {
			return {
				filters: {
					name: '',
                    date:'',
					platAccount : 0
				},
                record: [],
				total: 0,
				start:0,
                isShowKey: util.checkRoleSuper() ? true : false,
				pageSize:10,
                changeuuid:'',
				listLoading: false,
                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    price: [
                        {required: true, message: '不能为空', trigger: 'blur'}
                    ]
                     , remark: [
                        {required: true, message: '不能为空', trigger: 'blur'}
                    ]
                }, //新增界面数据
                addForm: {
                    price: '',
                    remark: ''
                }
			}
		},
		methods: {
            formatPayType: function (row, column) {
                return row.payType ==  1 ? '充值' : row.payType == 2 ? '扣款' : row.payType == 3 ? "支出" : '未知';
            },
            handleCurrentChange(val) {
				this.start = val - 1;
				this.getPlatFromAccountRecord();
			},
			handleSizeChange:function(val){
                this.pageSize = val;
                this.getPlatFromAccountRecord();
            },
			//获取变动记录
			getPlatFromAccountRecord() {
				let para = {
					start: this.start,
					name: this.filters.name,
                    pageSize:this.pageSize,
                    date: this.filters.date == "" ? "" : moment(this.filters.date,"YYYY-MM-DD HH:mm:ss").format('YYYY-MM-DD')
				};
				this.listLoading = true;
                getPlatFromAccountRecord(para).then( datas => {
					this.listLoading = false;
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.record = data.result;
                        this.total = data.totalCount;
                    }
				});
			},findPlatFromAccount(){
			    if(this.isShowKey){
                    findPlatFromAccount().then( datas => {
                        let { message, code, data } = datas;
                        if (code !== "0") {
                            this.$message({
                                message: message,
                                type: 'error'
                            });
                        } else {
                            this.filters.platAccount = data.platformamount+" 元";
                        }
                    });
				}
			} ,//显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    price: '',
                    remark: '',
                };
            }, //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            let para = Object.assign({}, this.addForm);
                            addPlatFormAccount(para).then((datas) => {
                                this.addLoading = false;
                                let { message, code, data } = datas;
                                if (code !== "0") {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }

                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.getPlatFromAccountRecord();
                                this.findPlatFromAccount();
                            });
                        });
                    }
                });
            }
		},
		mounted() {
		    this.findPlatFromAccount();
			this.getPlatFromAccountRecord();
		}
	}

</script>

<style scoped>

</style>
